<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:iotCustom:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:iotCustom:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:iotCustom:remove']"
        >删除
        </el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="customList" @selection-change="handleSelection">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="客户名称" prop="customName"  width="150"/>
      <el-table-column label="客户手机号" prop="customPhone" :show-overflow-tooltip="true" width="150"/>
      <el-table-column label="公司名称" prop="companyName"  :show-overflow-tooltip="true" width="150">
        <template slot-scope="scope">
          <div class="blue-font-color" @click="touchDetails(scope.row)">{{ scope.row.companyName }}</div>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope" v-if="scope.row.id !== 1">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="addCompany(scope.row)"
          >添加公司
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDetail(scope.row)"
          >操作记录
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改角色配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="客户名称" prop="customName">
          <el-input v-model="form.customName" placeholder="请输入客户名称"/>
        </el-form-item>
        <el-form-item label="客户手机号" prop="customPhone">
          <el-input v-model="form.customPhone" placeholder="请输入客户手机号" type="text" onkeyup="this.value=this.value.replace(/\D/g,'')"
                  maxlength="11"/>
        </el-form-item>
        <el-form-item label="公司名称" prop="companyName">
          <el-input v-model="form.companyName" placeholder="请输入公司名称"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!--  添加公司dialog  -->
    <el-dialog :title="openTitle" :visible.sync="openCompany" width="500px" append-to-body>
      <el-form ref="companyForm" :model="companyForm" label-width="100px">
        <el-form-item label="客户名称" prop="customName" >
          <el-input v-model="companyForm.customName" disabled="disabled" placeholder="请输入客户名称"/>
        </el-form-item>
        <el-form-item label="客户手机号" prop="customPhone">
          <el-input v-model="companyForm.customPhone" placeholder="请输入客户手机号" disabled="disabled" type="text" onkeyup="this.value=this.value.replace(/\D/g,'')"
                    maxlength="11"/>
        </el-form-item>
        <el-form-item label="公司名称" prop="companyName">
          <el-input v-model="companyForm.companyName" placeholder="请输入公司名称"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitCompany">确 定</el-button>
        <el-button @click="cancelCompany">取 消</el-button>
      </div>
    </el-dialog>
<!--    回显公司列表-->
    <el-dialog :title="deptTitle" :visible.sync="openDept" width="500px" append-to-body>
      <el-table v-loading="loading" :data="deptList" >
        <el-table-column label="公司名称" prop="deptName"  :show-overflow-tooltip="true" width="150"/>
        <el-table-column label="创建时间" align="center" prop="createTime" width="150">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope" v-if="scope.row.id !== 1">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="editDept(scope.row)"
            >修改
            </el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="deleteDept(scope.row)"
            >删除
            </el-button>

          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
    <OrUser :params=detailOrUser ref="toOrUser"/>
  </div>
</template>

<script>
import { listCustom, addCustom, getCustom, updateCustom, addCompany,deleteCustom,getDept } from '../../../api/system/custom'

import OrUser from './iotOrUser/index'
export default {
  name: 'index',
  components:{OrUser},
  data() {
    return {
      //操作记录Data
      detailOrUser:null,
      //客户下的所有企业
      deptList:[],
      openDept:false,
      deptTitle:"",
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 客户表格数据
      customList: [],
      // 非单个禁用
      single: true,
      // 弹出层标题
      title: '',
      // 总条数
      total: 0,
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层
      openCompany: false,
      //弹出层标题
      openTitle:'',
      // 日期范围
      dateRange: [],
      // 表单参数
      form: {},
      companyForm:{},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,

      },
      // 表单校验
      rules: {
        customName: [
          { required: true, message: '客户名称不能为空', trigger: 'blur' }
        ],
        customPhone: [
          {
            required: true,
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: '请输入正确的手机号码',
            trigger: ['blur', 'change']
          }
        ],
        companyName: [
          { required: true, message: '公司名称不能为空', trigger: 'blur' }
        ]
      },

    }
  },
  created() {
    this.getList()
  },

  methods: {
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    //
    cancelCompany(){
      this.openCompany = false
    },
    /** 查询客户列表 */
    getList() {
      this.loading = true
      listCustom(this.addDateRange(this.queryParams, this.dateRange)).then(res => {
        this.customList = res.rows
        this.total = res.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelection(selection) {
      this.ids = selection.map(item => item.roleId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加客户'
    },
    addCompany(row){
      this.openCompany = true
      this.openTitle = '添加公司'
      const id = row.id || this.ids
      getCustom(id).then(response => {
        this.companyForm=response.data
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCustom(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改客户";
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id || this.ids
      this.$modal.confirm('是否确认删除该客户？').then(function() {
        return deleteCustom(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 查看操作详情*/
     handleDetail(row){
      this.detailOrUser = row.id
       this.$nextTick(()=>{
         this.$refs.toOrUser.showDetailOr(this.detailOrUser)
       })
    },
    /** 查看该客户下的所有公司*/
    touchDetails(row){
      this.deptTitle="查看客户下所有公司"
      this.openDept=true
      getDept(row.customPhone).then(res=>{
        this.deptList=res.data
      })
    },
    /** 提交按钮 */
    submitCompany: function() {
      this.$refs["companyForm"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateCustom(this.companyForm).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.openCompany = false
              this.getList()
            })
          } else {
            addCompany(this.companyForm).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.openCompany = false
              this.getList()
            })
          }
        }
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateCustom(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addCustom(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        customName: '',
        customPhone: '',
        companyName: ''
      }
      this.resetForm('form')
    }
  }
}
</script>

<style scoped>

</style>
